package arina.sh.ethernetcontrol;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;


public class Relay{
	System_Device mSystem;
	int mID;
	int mNumberChannel;
	String mReadStatusURL;
	public myToggleButton[] ArrButton;
	public String Response;

	public byte[] udpResponse;
	public byte[] lastudpResponse;
	public String getReadStatusURL() {
		return this.mReadStatusURL;
	}

	public void setReadStatusURL(String readStatusURL) {
		this.mReadStatusURL = readStatusURL;
	}



	public Relay(System_Device system, int iD,int nChannel) {
		super();
		this.mSystem = system;
		this.mID = iD;
		this.mNumberChannel=nChannel;
		this.ArrButton=new myToggleButton[mNumberChannel];
		//		for(int i=0;i<mNumberChannel;i++)
		//			ArrButton[i].setTag("0");
		//default value: Local 
		this.mReadStatusURL="http://"+mSystem.getLocalIP()+":"+mSystem.getPort()+"/SMARTHOUSE/"+mSystem.getUsername()+"/"+mSystem.getPassword()+"/0033/14/fa";
		this.Response="";
		udpResponse=new byte[30];
		lastudpResponse=new byte[30];
	}

	public void On(int channel,NETWORK_STATUS mNetworkStatus)
	{
		mSystem.sendCommand(COMMAND_TYPE.ON_OFF, mID, channel, 100,mNetworkStatus);

	}
	public void Off(int channel,NETWORK_STATUS mNetworkStatus)
	{
		mSystem.sendCommand(COMMAND_TYPE.ON_OFF, mID, channel, 0,mNetworkStatus);
	}
	public void Reverse(int channel,NETWORK_STATUS mNetworkStatus)
	{

		mSystem.sendCommand(COMMAND_TYPE.REVERSE, mID, channel, 0,mNetworkStatus);

	}
	public void readStatusUDP(int channel,NETWORK_STATUS mNetworkStatus)
	{

		mSystem.sendCommand(COMMAND_TYPE.READ_STATUS, mID, channel, 0,mNetworkStatus);

	}
	public void readStatus(NETWORK_STATUS mNetworkStatus) throws IOException 
	{
		int connect_timeout=700;
		int read_timeout=650;
		String currIP=mSystem.getLocalIP();
		if(mNetworkStatus==NETWORK_STATUS.EXTERNAL)
		{
			currIP=mSystem.getExternalIP();
			connect_timeout=2500;
			read_timeout=2300;
		}

		String hexID=helperClass.decToHex(mID);
		String strURL="http://"+currIP+":"+mSystem.getPort()+"/SMARTHOUSE/"+mSystem.getUsername()+"/"+mSystem.getPassword()+"/0033/14/"+hexID.trim();
		String status="";
		URL IPURL = new URL(strURL);
		HttpURLConnection Client = (HttpURLConnection) IPURL.openConnection();
		InputStream is = null;
		try {
			Client.setConnectTimeout(connect_timeout);
			Client.setReadTimeout(read_timeout);
			//get inputstream before getResponsecode
			is=Client.getInputStream();
			int responseCode=Client.getResponseCode();

			if(responseCode!=200)
			{
				Response="Not Connected";
				return;
			}
			else
			{
				status= readIt(is,34);
			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			//e.printStackTrace();
		}
		finally{
			Client.disconnect();}


		Response=status.trim();//.substring(22);
	}

	private String readIt(InputStream stream, int len)  {
		Reader reader = null;
		char[] buffer = new char[len];

		try {
			reader = new InputStreamReader(stream, "UTF-8");
			try {
				reader.read(buffer);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return new String(buffer).trim();
	}
	public System_Device getSystem() {
		return this.mSystem;
	}
	public void setSystem(System_Device system) {
		this.mSystem = system;
	}
	public int getID() {
		return this.mID;
	}
	public void setID(int iD) {
		this.mID = iD;
	}
	public int getNumberChannel() {
		return this.mNumberChannel;
	}

	public void setNumberChannel(int numberChannel) {
		this.mNumberChannel = numberChannel;
	}



}
